load(
    "@bazel_skylib//rules:common_settings.bzl",
    "bool_flag",
    "string_flag",
)
load("@rules_pkg//:pkg.bzl", "pkg_tar")
load("@rules_erlang//:dialyze.bzl", "plt")
load("@rules_erlang//:shell.bzl", "shell")
load("@rules_erlang//:erl_eval.bzl", "erl_eval")
load("//bazel/elixir:iex_eval.bzl", "iex_eval")
load(":rabbitmq_home.bzl", "rabbitmq_home")
load(":rabbitmq_run.bzl", "rabbitmq_run", "rabbitmq_run_command")
load(":rabbitmqctl.bzl", "rabbitmqctl")
load(":dist.bzl", "package_generic_unix", "source_archive")
load(":rabbitmq.bzl", "all_plugins")

exports_files([
    "scripts/bazel/rabbitmq-run.sh",
    "scripts/bazel/rabbitmq-run.bat",
    "release-notes",
])

bool_flag(
    name = "enable_test_build",
    build_setting_default = False,
    visibility = ["//visibility:public"],
)

config_setting(
    name = "test_build",
    flag_values = {
        "//:enable_test_build": "true",
    },
)

string_flag(
    name = "elixir_home",
    build_setting_default = "",
    visibility = ["//visibility:public"],
)

plt(
    name = "base_plt",
    visibility = ["//visibility:public"],
)

PLUGINS = all_plugins(
    rabbitmq_workspace = "",
)

rabbitmq_home(
    name = "broker-home",
    plugins = PLUGINS,
)

rabbitmq_run(
    name = "rabbitmq-run",
    home = ":broker-home",
    visibility = ["//visibility:public"],
)

# Allow us to `bazel run broker`
# for the equivalent of `make run-broker`
rabbitmq_run_command(
    name = "broker",
    rabbitmq_run = ":rabbitmq-run",
    subcommand = "run-broker",
)

# Allow us to `bazel run start-cluster`
# for the equivalent of `make start-cluster`
rabbitmq_run_command(
    name = "start-cluster",
    rabbitmq_run = ":rabbitmq-run",
    subcommand = "start-cluster",
)

# Allow us to `bazel run stop-cluster`
# for the equivalent of `make stop-cluster`
rabbitmq_run_command(
    name = "stop-cluster",
    rabbitmq_run = ":rabbitmq-run",
    subcommand = "stop-cluster",
)

# `bazel run rabbitmqctl`
rabbitmqctl(
    name = "rabbitmqctl",
    home = ":broker-home",
    visibility = ["//visibility:public"],
)

rabbitmqctl(
    name = "rabbitmq-diagnostics",
    home = ":broker-home",
)

rabbitmqctl(
    name = "rabbitmq-plugins",
    home = ":broker-home",
)

rabbitmqctl(
    name = "rabbitmq-streams",
    home = ":broker-home",
)

rabbitmqctl(
    name = "rabbitmq-queues",
    home = ":broker-home",
)
shell(
    name = "repl",
    deps = PLUGINS,
)

erl_eval(
    name = "otp_version",
    outs = ["otp_version.txt"],
    expression = """{ok, Version} = file:read_file(filename:join([code:root_dir(), "releases", erlang:system_info(otp_release), "OTP_VERSION"])), file:write_file(os:getenv("OUTS"), Version), halt().""",
)

iex_eval(
    name = "elixir_version",
    outs = ["elixir_version.txt"],
    expression = """File.write!(System.get_env("OUTS"), System.version()); System.halt()""",
)

filegroup(
    name = "root-licenses",
    srcs = glob(
        ["LICENSE*"],
        exclude = [
            "LICENSE.md",
            "LICENSE.txt",
        ],
    ),
    visibility = ["//visibility:public"],
)

pkg_tar(
    name = "scripts-tar",
    srcs = [
        "scripts/bash_autocomplete.sh",
        "scripts/rabbitmq-script-wrapper",
        "scripts/rabbitmqctl-autocomplete.sh",
        "scripts/zsh_autocomplete.sh",
    ],
    package_dir = "scripts",
    visibility = ["//visibility:public"],
)

pkg_tar(
    name = "release-notes-tar",
    srcs = glob([
        "release-notes/*.md",
        "release-notes/*.txt",
    ]),
    package_dir = "release-notes",
    visibility = ["//visibility:public"],
)

package_generic_unix(
    plugins = PLUGINS,
    rabbitmq_workspace = "@",
)

source_archive(
    plugins = PLUGINS,
    rabbitmq_workspace = "@",
)

alias(
    name = "test-logs",
    actual = "//bazel/util:test-logs",
)

alias(
    name = "remote-test-logs",
    actual = "//bazel/util:remote-test-logs",
)

alias(
    name = "test-node-data",
    actual = "//bazel/util:test-node-data",
)

alias(
    name = "remote-test-node-data",
    actual = "//bazel/util:remote-test-node-data",
)
